﻿#include "IceCrystalSoldier.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    IceCrystalSoldier::IceCrystalSoldier(MonsterInfo *info) : MonsterObject(info)
    {
    }

    void IceCrystalSoldier::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        if (getEnvir()->getTime() < _areaTime)
        {
            if (getEnvir()->Random->Next(4) == 0)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                tempVar->Type = 1;
                Broadcast(tempVar);
                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage * 3 / 2, DefenceType::ACAgility});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                MonsterObject::Attack();
            }
            return;
        }

        _areaTime = getEnvir()->getTime() + 2000 + getEnvir()->Random->Next(5) * 1000;

        S::ObjectRangeAttack *tempVar2 = new S::ObjectRangeAttack();
        tempVar2->ObjectID = ObjectID;
        tempVar2->Direction = getDirection();
        tempVar2->Location = getCurrentLocation();
        Broadcast(tempVar2);
        DelayedAction tempVar3(DelayedType::RangeDamage, getEnvir()->getTime() + 500);
        ActionList.push_back(&tempVar3);

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
    }

    void IceCrystalSoldier::CompleteRangeAttack(std::vector<std::any> &data)
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]) * 2;
        if (damage == 0)
        {
            return;
        }

        std::vector<MapObject*> targets = FindAllTargets(1, Functions::PointMove(getCurrentLocation(), getDirection(), 1));
        if (targets.empty())
        {
            return;
        }

        for (int i = 0; i < targets.size(); i++)
        {
            targets[i]->Attacked(this, damage, DefenceType::MAC);
        }
    }
}
